package com.cysyz.mylib.utils;

import org.omg.CORBA.Object;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *  日期时间处理类 功能：日期时间的处理可以获得星期
 */
public class DateUtil {
    public final static int SECONDS_DAY = 86400;
    /**
     * 获取星期名称
     *
     * @param date
     *            日期
     * @return
     */
    public static String getDayweek(Date date) {
        String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayinweek = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return weeks[dayinweek];
    }

    /**
     * 获得日期的年月日格式 如：2012年6月4日
     *
     * @return
     */
    public static String getDateForymd() {
        Calendar ca = Calendar.getInstance();
        int year = ca.get(Calendar.YEAR);// 获取年份
        int month = ca.get(Calendar.MONTH) + 1;// 获取月份 默认1月为：0,所以要加一
        int day = ca.get(Calendar.DATE);// 获取日
        int minute = ca.get(Calendar.MINUTE);// 分
        int hour = ca.get(Calendar.HOUR);// 小时
        int second = ca.get(Calendar.SECOND);// 秒
        int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
        String dateString = year + "年" + month + "月" + day + "日";
        return dateString;
    }

    /**
     * 获取当前日期起始周(星期一)
     *
     * @param date
     *            日期
     * @return
     */
    public static Date getWeekStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DAY_OF_WEEK) - 1;
        cal.add(Calendar.DATE, -day + 1);
        return cal.getTime();
    }

    /**
     * 获取当前日期周末(星期日)
     *
     * @param date
     *            日期
     * @return
     */
    public static Date getWeekEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DAY_OF_WEEK) - 1;
        cal.add(Calendar.DATE, 6 - day + 1);
        return cal.getTime();
    }

    /**
     * 得到当前时间的INT类型
     */
    public static int getDateTime() {
        return Integer
                .parseInt(Long.toString(System.currentTimeMillis() / 1000));
    }

    public static long getLongDateTime()
    {
        return System.currentTimeMillis() / 1000;
    }
    /**
     * 转换int时间到timestamp
     *
     * @param seconds
     * @return
     */
    public static Timestamp getTimestamp(int seconds) {
        return new Timestamp(Long.parseLong(Integer.toString(seconds)) * 1000);
    }

    public static Timestamp getTimestamp(long seconds) {
        return new Timestamp(seconds * 1000);
    }
    /**
     * 得到时间格式展示
     * @param seconds Int型毫秒数
     * @param format 格式作为参数接收
     * @return
     */
    public static String getTimeString(int seconds,String format){
        Timestamp timestamp = new Timestamp(Long.parseLong(Integer.toString(seconds)) * 1000);
        String timeStr = changeTimeToString(timestamp, format);
        return timeStr;
    }

    /**
     * 得到时间格式展示,格式为2012-12-21
     * @param seconds Int型毫秒数
     * @return
     */
    public static String getTimeString(int seconds){
        Timestamp timestamp = new Timestamp(Long.parseLong(Integer.toString(seconds)) * 1000);
        String timeStr = changeTimeToString(timestamp, "yyyy-MM-dd");
        return timeStr;
    }

    public static String getTimeString(long seconds,String format){
        Timestamp timestamp = new Timestamp(seconds * 1000);
        String timeStr = changeTimeToString(timestamp, format);
        return timeStr;
    }

    /**
     * 将String时间转换成int类型
     *
     * @param timeStr
     * @return
     */
    public static int getIntTime(String timeStr) {
        return DateUtil.getIntDateTime(timeStr);
    }

    /**
     * 取得日期对应的距离1970年1月1日的秒数
     * @Title: getIntDateTime
     * @Author: 崔勇
     * @Version: V1.00
     * @Date 2014-4-28 下午3:32:30
     * @param date 日期 格式为 yyyy-MM-dd
     * @return 日期对应的距离1970年1月1日的秒数
     */
    public static int getIntDateTime(String date) {
        return DateUtil.getIntDateTime(date, BeginOrEnd.begin);
    }

    /**
     * 取得日期对应的距离1970年1月1日的秒数
     * @Title: getIntDateTime
     * @Author: 崔勇
     * @Version: V1.00
     * @Date 2014-4-28 下午3:32:30
     * @param date 日期 格式为 yyyy-MM-dd
     * @param beginOrEnd
     * @return 日期对应的距离1970年1月1日的秒数
     */
    public static int getIntDateTime(String date, BeginOrEnd beginOrEnd) {
        if ((date != null)&&(!date.equals(""))) {
            String time = null;
            if (BeginOrEnd.begin == beginOrEnd) {
                time = " 00:00:00";
            } else if (BeginOrEnd.end == beginOrEnd) {
                time = " 24:00:00";
            }
            Timestamp timeStamp = stringToTimestamp(date + time);
            Long timeTemp = timeStamp.getTime() / 1000;
            int dateTimeInt = Integer.valueOf(timeTemp.toString());
            return dateTimeInt;
        }
        return 0;
    }

    public static enum BeginOrEnd {
        begin, end;
    }

    /**
     * 取得距离今天几天前或几天后的日期的数字格式
     * @Title: getIntDateTime
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014年5月19日 下午2:45:22
     * @param diffDay
     * @return
     */
    public static int getIntDateTime(int diffDay, BeginOrEnd beginOrEnd) {
        return DateUtil.getIntDateTime(DateUtil.getStringDateTime(diffDay), beginOrEnd);
    }

    /**
     * 取得距离今天几天前或几天后的日期的数字格式
     * @Title: getIntDateTime
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014年5月19日 下午2:45:22
     * @param diffDay
     * @return
     */
    public static int getIntDateTime(int diffDay) {
        return DateUtil.getIntDateTime(DateUtil.getStringDateTime(diffDay));
    }

    public static long getLast7dayIntTime(){
        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -7);  //设置为前7天
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 取得今天之前或之后的某一天，日期格式如：2012-05-01
     * @Title: getStringDateTime
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014-5-4 上午10:35:12
     * @param diffday -1代表之前一天，-2代表之前二天，+1代表明天
     * @return
     */
    public static String getStringDateTime(int diffday){
        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, diffday);  //设置为前7天
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND, 0);
        return getTimeString(calendar.getTimeInMillis() / 1000, "yyyy-MM-dd");
    }

    /**
     * 取得今天之前或之后的某一天
     * @Title: getStringDateTime
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014-5-4 上午10:35:12
     * @param diffday -1代表之前一天，-2代表之前二天，+1代表明天
     * @return
     */
    public static Date getDateTime(int diffday){
        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, diffday);  //设置为前7天
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    /**
     * 取得某天之前或之后的某一天
     * @Title: getStringDateTime
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014-5-4 上午10:35:12
     * @param referenceDate 某一日期，计算基于此日期
     * @param diffday -1代表之前一天，-2代表之前二天，+1代表明天
     * @return
     */
    public static Date getDateTime(Date referenceDate, int diffday){
        if (referenceDate == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(referenceDate);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, diffday);  //设置为前7天
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    /**
     * 取得某天之前或之后的某一天
     * @Title: getStringDateTime
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014-5-4 上午10:35:12
     * @param referenceDate 某一日期，计算基于此日期
     * @param diffday -1代表之前一天，-2代表之前二天，+1代表明天
     * @return
     */
    public static String getStringDateTime(Date referenceDate, int diffday){
        Date dateTime = DateUtil.getDateTime(referenceDate, diffday);
        return getTimeString(dateTime.getTime() / 1000, "yyyy-MM-dd");
    }

    public static String getStringDateTime(String dateStr, String format, int diffday){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(date);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, diffday);  //设置为前7天
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return getTimeString(calendar.getTimeInMillis() / 1000, "yyyy-MM-dd");
    }

    public static long getLast2hourIntTime(){
        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.HOUR_OF_DAY, -2);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTimeInMillis() / 1000 ;
    }

    public static long getLongTime(String timeStr) {
        if ((timeStr != null)&&(!timeStr.equals(""))) {
            Timestamp timeStamp = stringToTimestamp(timeStr + " 00:00:00");
            Long timeTemp = timeStamp.getTime() / 1000;
            long time = Long.valueOf(timeTemp.toString());
            return time;
        }
        return 0;
    }

    /**
     * 字符串转换为Timestamp
     *
     * @param timestampStr
     * @param format
     * @return
     */
    public static Timestamp stringToTimestamp(String timestampStr, String format) {
        Timestamp ts = null;
        if (timestampStr != null && !timestampStr.trim().equals(" ")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            try {
                Date date = dateFormat.parse(timestampStr);
                ts = new Timestamp(date.getTime());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return ts;
    }

    /**
     * 字符串转换为Timestamp
     *
     * @param timestampStr
     * @return
     */
    public static Timestamp stringToTimestamp(String timestampStr) {
        Timestamp ts = null;
        if (timestampStr != null && !timestampStr.trim().equals(" ")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            try {
                Date date = dateFormat.parse(timestampStr);
                ts = new Timestamp(date.getTime());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return ts;
    }

    /**
     * 转换日期格式
     *
     * @param timestamp
     * @return
     */
    public static String changeTimeToString(Timestamp timestamp) {
        String datestr = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        datestr = dateFormat.format(timestamp);
        return datestr;
    }

    /**
     * 转换日期格式
     *
     * @param timestamp
     * @return
     */
    public static String changeTimeToStringDate(Timestamp timestamp, String format) {
        String datestr = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        datestr = dateFormat.format(timestamp);
        return datestr;
    }

    /**
     * 根据Object类型转换成有格式的String日期。 主要是在mobile部分的VO构造函数中使用
     *
     * @return
     */
    public static String changeObjectToStringDate(Object object) {
        Timestamp timestamp = stringToTimestamp(object.toString());
        String datestr = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy/MM/dd HH:mm:ss");
        datestr = dateFormat.format(timestamp);
        return datestr;
    }

    /**
     * 转换日期格式 根据自定义格式
     *
     * @param timestamp
     * @return
     */
    public static String changeTimeToString(Timestamp timestamp, String format) {
        String datestr = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        datestr = dateFormat.format(timestamp);
        return datestr;
    }

    /**
     * 获得当前时间
     *
     * @return Timestamp 类型
     */
    public static Timestamp getCurrentTime() {
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        return timestamp;
    }

    /**
     * 转换Date类型为String类型的对象
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String changeDateTimeToString(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
     * 取得今天的日期的字符串形式
     * @Title: getCurrentDate
     * @Author: 崔勇	cysyz1229@gmail.com
     * @Version: V1.00
     * @Date 2014年5月19日 下午4:14:36
     * @return
     */
    public static String getCurrentDate(){
        return changeDateTimeToString(new Date(), "yyyy-MM-dd");
    }
    public static String getCurrentDate(String format){
        return changeDateTimeToString(new Date(), format);
    }

    public static int getPriorHourTime( int hour) {
        Calendar now = Calendar.getInstance();
        now.add( Calendar.HOUR_OF_DAY, hour );
        return Integer.parseInt(Long.toString(now.getTimeInMillis() / 1000));
    }

    public static int getPriorMonthTime(int month){
        Calendar now = Calendar.getInstance();
        now.add( Calendar.MONTH, month );
        return getIntTime(changeDateTimeToString(now.getTime(), "yyyy-MM-dd"));
    }

    public static int getPriorDateTime(int date){
        Calendar now = Calendar.getInstance();
        now.add( Calendar.DAY_OF_MONTH, date );
        return getIntTime(changeDateTimeToString(now.getTime(),"yyyy-MM-dd"));
    }

    public static String getTimeForAjax(Integer inttime) {
        String format = "yyyy-MM-dd";
        if (inttime != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(DateUtil.getTimestamp(inttime
                    .intValue()));

        } else {
            return "";
        }
    }

    public static String format(Date date, String format) {
        String retVal = "";
        if (date != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            retVal = simpleDateFormat.format(date);
        }
        return retVal;
    }

    /**
     * 比较oDate - fDate的相差天数
     * @param fDate 第一个日期
     * @param oDate 第二个日期
     * @return 两个日期的相差天数
     */
    public static int daysOfTwo(Date fDate, Date oDate) {

        Calendar aCalendar = Calendar.getInstance();

        aCalendar.setTime(fDate);

        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

        aCalendar.setTime(oDate);

        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

        return day2 - day1;

    }

    /**
     *字符串的日期格式的计算
     */
    public static int daysBetween(String smdate,String bdate) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }



    public static void main(String[] args) {
//    	System.out.println("aadfad and ".substring(0, "aadfad and ".length() -5));
        System.out.println(getStringDateTime(-1));
//    	System.out.println(changeTimeToStringDate(getTimestamp(1394588101),"yyyy-MM-dd"));
    }
}

